clear all 
set more off

global mydir "C:\XYZ" // set working directory

global temp "$mydir\data\temporary_output"
cd "$mydir\data\distance"
set matsize 11000
set maxvar 32700


/* The code is commented out because you need to register a free account with HERE
* to obtain hereid and herecode when using georoute to calculate driving distance
*https://developer.here.com/
******************************************************************************
**** FILL IN PAIR OF COUNTY-FACILITY AND CALCULATE DISTANCE DRIVING ************
use "$temp/trash_flow_county_facility_pair_profile.dta", clear
gen posq = 1
label var posq "dummy =1 if positive trash pair"
preserve
gen expcnty = 1 if facility=="oos"
label var expcnty "dummy=1(county has exported trash)"
keep if facility=="oos"
keep county expcnty
sort county
save expcnty, replace
restore
gen Code = "."
merge m:1 Code using "$mydir\data\price_confidential_data/WBJORNVAZProfile_2016.dta"
drop _merge
replace facility= Code if facility==""
replace deslat = Latitude if facility==""
replace deslon = Longitude if facility==""
rename County WBJcnty
keep county facility swis Code Permit State descnty WBJcnty posq
*make pairs
fillin facility county
drop if county==""
drop if facility=="oos"
replace posq = 0 if posq==.
keep county facility posq

* merge back to expcnty to get dummy for exported county and posq for exports
sort county
merge m:1 county using expcnty
drop _merge
erase expcnty.dta

* merge back to county profile to get county characteristics
sort county 
merge m:1 county using "$mydir\data\demographics/county_population_weighted_coordinate.dta"
drop if _merge==2
drop _merge

* merge back to trashflow facility profile to get fa charac
sort facility 
merge m:1 facility using "$temp/trash_flow_facility_profile_complete.dta"
drop _merge

*merge back to WBJ out of state profile to get oos charac
sort facility
rename facility Code 
merge m:1 Code using "$mydir\data\price_confidential_data/WBJORNVAZProfile_2016.dta"
drop _merge
gen oos = (deslat==.)
replace deslat = Latitude if deslat==.
replace deslon = Longitude if deslon ==.
replace descnty = County if descnty==""
rename Code facility
gen Code = facility if oos==1
replace Code ="" if oos==0
keep county facility posq expcnty Code cpwlat cpwlon ///
	zipcode deslat deslon swis descnty State oos
label var oos "dummy=1(facility is outside of state)"
replace expcnty= 0 if expcnty==.
replace posq = . if expcnty==1 & (State=="AZ" |State=="OR"|State=="NV")
*cal Vdistance
geodist cpwlat cpwlon deslat deslon, miles gen(Vdis)

*** LEAVE OUT FACILITIES ON ISLANDS
gen island=0
replace island=1 if swis=="19-AA-0061"
replace island=1 if swis=="19-AA-0062"
replace island=1 if swis=="19-AA-0063"
keep if island==0
save all_pairs_to_calculate_distance, replace

*****************************************************************************
** Calculate all pairs in CA; for oos, just calculate pairs <=500 miles
** The limit is because HERE API allows free use of 50,000 observations
keep if oos==0 | (oos==1 & Vdis<=500)
sort facility county // to check coordinates of all counties, calculate first 71 obs.

* calculate driving distance by car (miles) and driving time (in minutes) by car and in
* normal traffic, using HERE map https://developer.here.com/
** Register a free account in HERE by an email address. Then get the hereid and
*herecode to input into the below code
georoute, hereid(mLpBJj9MDOnMxJm1SuQs) herecode(J1PXO5-JSc57Ff9kv0_NKQ) ///
	   startxy(cpwlat cpwlon) endxy(deslat deslon) distance(dridis) ///
	   time(dritime) coordinates(start end) replace

*attached this info back to the original data
tempfile tmp
save `tmp', replace
use all_pairs_to_calculate_distance, clear
sort facility county
merge 1:1 facility county using `tmp'
erase `tmp'
save all_pairs_to_calculate_distance_done, replace // ########### FINAL FILE 
erase all_pairs_to_calculate_distance.dta
